using System;
using System.Collections.Generic;
using System.Text;

namespace TileGert.Particles
{
  /// <summary>
  /// GertParticle is a simple particle that is used by the particle system.
  /// </summary>
  public class GertParticle : GertSprite
  {
    private GertVector _velocity = new GertVector();
    private float _lifeTime = 10.0f;

    /// <summary>
    /// Creates a new particle.
    /// </summary>
    public GertParticle()
    {
    }
    
    /// <summary>
    /// Creates a new particle and initializes the position with the given values.
    /// </summary>
    /// <param name="x">The x coordinate of the top-left-corner.</param>
    /// <param name="y">The y coordinate of the top-left-corner.</param>
    public GertParticle(float x, float y)
    {
      _position = new GertPoint(x, y);
    }
    
    /// <summary>
    /// Creates a new particle and initializes the position and velocity with the given values.
    /// </summary>
    /// <param name="x">The x coordinate of the top-left-corner.</param>
    /// <param name="y">The y coordinate of the top-left-corner.</param>
    /// <param name="vx">The x value of the velocity.</param>
    /// <param name="vy">The x value of the velocity.</param>
    public GertParticle(float x, float y, float vx, float vy)
    {
      _position = new GertPoint(x, y);
      _velocity = new GertVector(vx, vy);
    }

    /// <summary>
    /// Creates a new particle and initializes the position and velocity with the given values.
    /// </summary>
    /// <param name="position">The position of the top-left-corner.</param>
    /// <param name="velocity">The velocity.</param>
    public GertParticle(GertPoint position, GertVector velocity)
    {
      _position = position;
      _velocity = velocity;
    }

    /// <summary>
    /// Creates a new particle and initializes its properties with the given values.
    /// </summary>
    /// <param name="position">The position of the top-left-corner.</param>
    /// <param name="velocity">The velocity.</param>
    /// <param name="image">The to be drawn.</param>
    /// <param name="lifetime">The lifetime in seconds.</param>
    public GertParticle(GertPoint position, GertVector velocity, string image, float lifetime)
    {
      _position = position;
      _velocity = velocity;
      _image = image;
      _lifeTime = lifetime;
    }
    
    /// <summary>
    /// Gets or sets the velocity of the particle measured in pixels per second.
    /// </summary>
    public GertVector Velocity
    {
      get { return _velocity; }
      set { _velocity = value; }
    }

    /// <summary>
    /// Sets the life time of the particle measured in seconds. 
    /// When the particles life time has dropped to zero, it will
    /// not be updated anymore.
    /// </summary>
    public float LifeTime
    {
      get { return _lifeTime; }
      set { _lifeTime = value; }
    }

    /// <summary>
    /// Checks if the particle is still alife.
    /// </summary>
    /// <returns>True if the particle is still alife, else false.</returns>
    public bool IsAlife()
    {
      return _lifeTime > 0.0f;
    }
    
    /// <summary>
    /// Updates the position and life time of the particle taking into account the time step.
    /// </summary>
    /// <param name="timestep">The time to advance the particle in seconds.</param>
    public void Update(float timestep)
    {
      _position.Transform(_velocity, timestep);
      _lifeTime -= timestep;
    }

    /// <summary>
    /// Can be used to set all properties of a particle in one step.
    /// </summary>
    /// <param name="position">The position of the top-left-corner.</param>
    /// <param name="velocity">The velocity.</param>
    /// <param name="image">The to be drawn.</param>
    /// <param name="lifetime">The lifetime in seconds.</param>
    public void Initialize(GertPoint position, GertVector velocity, string image, float lifetime)
    {
      _position = position;
      _velocity = velocity;
      _image = image;
      _lifeTime = lifetime;
    }

  }
}
